package com.store.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.store.app.BaseActivity;
import com.store.app.MainActivity;
import com.store.app.R;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.MoneyUtil;
import com.store.app.utils.Utils;

import org.json.JSONObject;


/**
 * Created by Administrator on 2017/5/5.
 * 礼券兑换页面
 */
public class CoupleConsume extends BaseActivity implements View.OnClickListener, IOnHttpListener {
    private ImageView public_ll_return;
    private TextView tvTitle;
    private TextView tv_query;
    private TextView tv_name_phone;
    private EditText et_phone;
    private TextView tv_get_code;
    private TextView tv_code;
    private EditText et_code;
    private TextView tv_duihuan;
    private EditText et_duihuan;
    private TextView tv_yue;
    private TextView tv_balance;
    private Button bt_submit;
    private NetRequest netRequest;
    private String consumer_id;
    private TextView tvRight;
    private EditText edit_sendcode;
    private TextView tv_sendcode;
    private String phone;
    private CountDownTimer downtimer;//倒计时
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_couple_consume);
        netRequest = new NetRequest(this);
        initView();
    }

    private void initView() {
        public_ll_return = (ImageView) findViewById(R.id.public_ll_return);
        tvRight = (TextView) findViewById(R.id.public_tv_right);
        tvRight.setText("历史记录");
        tvRight.setOnClickListener(this);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        tvTitle.setText("礼券兑换");
        tv_query = (TextView) findViewById(R.id.tv_query);
        tv_name_phone = (TextView) findViewById(R.id.tv_name_phone);
        et_phone = (EditText) findViewById(R.id.et_phone);
        tv_duihuan = (TextView) findViewById(R.id.tv_duihuan);
        et_duihuan = (EditText) findViewById(R.id.et_duihuan);
        tv_yue = (TextView) findViewById(R.id.tv_yue);
        tv_balance = (TextView) findViewById(R.id.tv_balance);
        bt_submit = (Button) findViewById(R.id.bt_submit);
        edit_sendcode=(EditText) findViewById(R.id.edit_sendcode);
        tv_sendcode= (TextView) findViewById(R.id.tv_send_code);
        tv_sendcode.setOnClickListener(this);
        public_ll_return.setOnClickListener(this);
        bt_submit.setOnClickListener(this);
        tv_query.setOnClickListener(this);
        downtimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tv_sendcode.setText((millisUntilFinished / 1000) + "秒后重发");
            }

            @Override
            public void onFinish() {
                tv_sendcode.setEnabled(true);
                tv_sendcode.setText("获取验证码");
            }
        };
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_send_code:
                sendCode();
                break;
            case R.id.public_tv_right:
                Intent intent = new Intent(this,CoupleExchangeHistoryActivity.class);
                startActivity(intent);
                break;
            case R.id.public_ll_return:
                finish();
                break;
            case R.id.bt_submit:
                if (Utils.isFastClick()) {
                    return;
                }
                if (TextUtils.isEmpty(et_phone.getText().toString().trim())) {
                    showToast("请输入手机号");
                    return;
                }
                if (!Utils.matchPhone(et_phone.getText().toString().trim())) {
                    showToast("请输入正确手机号");
                    return;
                }
                if (TextUtils.isEmpty(et_duihuan.getText().toString().trim())) {
                    showToast("请输入兑换数量");
                    return;
                }
                if (et_duihuan.getText().toString().trim().equals("0")) {
                    showToast("兑换数量不能为0");
                    return;
                }
                if (TextUtils.isEmpty(tv_balance.getText().toString().trim())) {
                    showToast("请先点击查找获取用户礼券余额");
                    return;
                }

                //只有余额大于等于兑换数才可以去兑换
                if (!MoneyUtil.moneyComp(tv_balance.getText().toString().trim(), et_duihuan.getText().toString().trim())) {
                    showToast("礼券余额不足");
                    return;
                }
                if(TextUtils.isEmpty(edit_sendcode.getText().toString().trim())){
                    showToast("请输入验证码");
                    return;
                }
                if (!TextUtils.isEmpty(consumer_id)) {
                    showProgressDialog("","正在兑换礼券...",false);
                    netRequest.requestVoucherExchange(3, consumer_id, et_phone.getText().toString().trim(), et_duihuan.getText().toString().trim()
                    ,edit_sendcode.getText().toString().trim());
                }

                break;
            case R.id.tv_query:
                String phone = et_phone.getText().toString().trim();

                if (TextUtils.isEmpty(phone)) {
                    showToast("请输入手机号");
                    return;
                }
                if (!Utils.matchPhone(phone)) {
                    showToast("请输入正确手机号");
                    return;
                }
                showProgressDialog("","正在查找...");
                netRequest.requestVailsMemberExist(1, phone);
                break;
        }
    }

    @Override
    public void onAfinalFail() {
        dismissProgressDialog();
        showToast("请检查本地网络！");
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {

        if (requestId == 1) {
            dismissProgressDialog();
            try {
                JSONObject jsonObject = new JSONObject(result);
                consumer_id = jsonObject.getString("consumer_id");
            } catch (Exception e) {
                e.printStackTrace();
            }
            netRequest.requestQueryCoupleBalance(2, et_phone.getText().toString().trim());
        } else if (requestId == 2) {
            dismissProgressDialog();
            try {
                JSONObject jsonObject = new JSONObject(result);
                String voucher = jsonObject.getString("voucher");
                tv_balance.setText(voucher);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestId == 3) {
            showToast("兑换成功");
            et_phone.setText("");
            et_duihuan.setText("");
            tv_balance.setText("");
            dismissProgressDialog();
            finish();
        } else if (requestId == 5){
            //获取验证码信息正确后 获取短信验证码
            netRequest.requestSendSmsCode(4, MainActivity.app_token, phone,result);
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        dismissProgressDialog();
        showToast(msg);
        //获取验证码失败
        if (requestId == 4||requestId == 5) {
            tv_sendcode.setEnabled(true);
            downtimer.cancel();
            downtimer.onFinish();
        }
    }

    private void sendCode() {
        phone = et_phone.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(CoupleConsume.this, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!Utils.matchPhone(phone)) {
            Toast.makeText(CoupleConsume.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(et_duihuan.getText().toString().trim())) {
            showToast("请输入兑换数量");
            return;
        }
        if (et_duihuan.getText().toString().trim().equals("0")) {
            showToast("兑换数量不能为0");
            return;
        }
        if (TextUtils.isEmpty(tv_balance.getText().toString().trim())) {
            showToast("请先点击查找获取用户礼券余额");
            return;
        }

        //只有余额大于等于兑换数才可以去兑换
        if (!MoneyUtil.moneyComp(tv_balance.getText().toString().trim(), et_duihuan.getText().toString().trim())) {
            showToast("礼券余额不足");
            return;
        }
        downtimer.start();
        tv_sendcode.setEnabled(false);
        edit_sendcode.requestFocus();
        //获取短信验证码验证信息
        netRequest.requestSendSmsCodeMess(5,MainActivity.app_token);

    }

}
